package exttable.testversioning.testversionrunner;

import exttable.testversioning.IDBUtils;
import exttable.testversioning.TestVersioningDBUtils;
import exttable.view.ViewAnalysis;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Jun 15, 2010
 * Time: 5:21:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class TestVersionsEditor extends AbstractCellEditor implements TableCellEditor {

    public static final int ANALYSIS = 0;
    public static final int VERSION = 2;
    public static final int PHASE = 3;

    private JComboBox cb;
    private JButton button;
    private IDBUtils dataService = new TestVersioningDBUtils();


    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        ViewAnalysis va = (ViewAnalysis) table.getValueAt(row, 0);
        switch (column) {
            case ANALYSIS:
                return getButtonToCreateVersion(va);
            case VERSION:
                return getComboWithVersions(va);
            case PHASE:
                return getComboBoxWithPhases();
            default:
                throw new IllegalArgumentException("no such value");
        }
    }

    @Override
    public Object getCellEditorValue() {
        return cb.getSelectedItem();
    }

    private JComboBox getComboWithVersions(ViewAnalysis analysis) {
        cb = new JComboBox(dataService.getVersions(analysis));
        if (cb.getItemCount() > 0) {
            return cb;
        } else {
            cb.setEnabled(false);
            return cb;
        }
    }

    private JComboBox getComboBoxWithPhases() {
        cb = new JComboBox(dataService.getPhases());
        return cb;
    }

    private JButton getButtonToCreateVersion(ViewAnalysis analysis) {
        button = new JButton(analysis.toString());
        return button;
    }
}
